
import java.util.Comparator;
import java.util.TreeSet;

public class ComparatorDemo {
    public static void main(String[] args) {
 TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {  //使用匿名内部类实现Comparator比较器
     @Override
     public int compare(Student s1, Student s2) {
         int num = s1.getAge() - s2.getAge();
         int ret = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
         return ret;
     }
 });
        ts.add(new Student("Shi Xi",27));
        ts.add(new Student("Chao Diao" , 25));
        ts.add(new Student("Zhaojun Wang",27));
        ts.add(new Student("Yuhuan Yang" , 29));

        for(Student stu : ts)
            System.out.println(stu.getName() +" , " + stu.getAge());
    }


}

